package com.csw.jdbc.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csw.jdbc.dao.TbPermissionDao;
import com.csw.jdbc.entity.TbPermission;
import com.csw.jdbc.entity.TbUser;
import com.csw.jdbc.dao.TbUserDao;
import com.csw.jdbc.service.TbUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
 
 
 /**
 * 用户表(TbUser)表服务实现类
 * @author chengshengwen
 * @description 
 * @since 2021-10-31 23:27:01
 */
@Service
@Transactional
@AllArgsConstructor
public class TbUserServiceImpl implements TbUserService,UserDetailsService{


    private final TbUserDao tbUserDao;

    private final TbPermissionDao tbPermissionDao;
 
    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbUser queryById(Long id) {
        return tbUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     * @param tbUser 实例对象
     * @return 对象列表
     */
    @Override
    public List<TbUser> queryAll(TbUser tbUser) {
        //PageHelper.startPage(pageNum,pageSize);
        List<TbUser> dataList = tbUserDao.queryAll(tbUser);
        //PageInfo<TbUser> page = new PageInfo<TbUser>(dataList);
        return dataList;
    }
 
    /**
     * 新增数据
     * @param tbUser 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(TbUser tbUser) {
        return tbUserDao.insert(tbUser);
    }
 
    /**
     * 修改数据
     * @param tbUser 实例对象
     * @return 实例对象
     */
    @Override
    public int updateById(TbUser tbUser) {
        return tbUserDao.updateById(tbUser);
    }
 
    /**
     * 通过主键id删除数据
     * @param id 主键
     */
    @Override
    public int deleteById(Long id) {
        return tbUserDao.deleteById(id);
    }

     @Override
     public TbUser getByUsername(String username) {
         QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
         wrapper.eq("username",username);
         return this.tbUserDao.selectOne(wrapper);
     }

     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //从数据库查用户
        TbUser user = getByUsername(username);
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        if(user != null) {
            List<TbPermission> tbPermissions = tbPermissionDao.selectByUserId(user.getId());

            //设置权限
            tbPermissions.forEach(permissions -> {
                if(permissions != null && !StrUtil.isEmpty(permissions.getEnname())) {
                    SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permissions.getEnname());
                    authorities.add(grantedAuthority);
                }
            });
            //封装成UserDetails的实现类
            return new org.springframework.security.core.userdetails.User(
                    user.getUsername(),user.getPassword(),authorities);
        }else {
            throw new UsernameNotFoundException("用户名不存在");
        }
     }
 }